﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using GameSPK.Helpers;
using GameSPK.DAO;

namespace GameSPK.Content
{
    /// <summary>
    /// Interaction logic for frmChonNguoiThi.xaml
    /// </summary>
    public partial class frmChonNguoiThi : Window
    {
        public frmChonNguoiThi()
        {
            InitializeComponent();
        }
        ObservableCollection<INguoiChoi> taphop;
       
        private void ShowData()
        {
            //this.uNguoiChoi1.DataContext = null;
            if (taphop == null)
                return;
            //if (taphop.Count <= 0)
            //    return;
            try
            {
                this.uNguoiChoi1.DataContext = new NguoiChoiCollection { TapHopNguoiChoi = taphop };
            }
            catch
            {
                return;
            }
        }
        private void uNguoiChoi1_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            taphop = NguoiChoiDAO.getNguoiChoiDaChon(MainWindow.getCuocThiChon());
            if (taphop.Count() <= 0)
                return;
            ShowData();
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            if (cboChoose.SelectedIndex == 1)
            {
                if(NguoiChoiDAO.ThemNguoiChoiCuocChoi(taphop, MainWindow.getCuocThiChon()))
                    MessageBox.Show("Đã chọn xong");
            }
            else
            {
                if (NguoiChoiDAO.UpdateNguoiChoiPhanThi(taphop, MainWindow.getCuocThiChon()))
                    MessageBox.Show("Đã chỉnh sửa xong");
            }
        }
        
        private void cboChoose_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            
            if (cboChoose.SelectedIndex == 0)
            {
                frmNhapNguoiChoi nhapnguoichoi = new frmNhapNguoiChoi();
                nhapnguoichoi.ShowDialog();
                
                taphop = NguoiChoiDAO.getNguoiChoiDaChon(MainWindow.getCuocThiChon());
                ShowData();
            }
            else if (cboChoose.SelectedIndex == 1)
            {
                taphop = NguoiChoiDAO.getNguoiChoiTheoTen(MainWindow.getCuocThiChon(), this.txtSearch.Text.Trim());
                ShowData();
            }
            else if (cboChoose.SelectedIndex == 2)
            {
                taphop = NguoiChoiDAO.getNguoiChoiDaChon(MainWindow.getCuocThiChon());
                ShowData();
            }
        }

        private void txtSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            taphop = NguoiChoiDAO.getNguoiChoiTheoTen(MainWindow.getCuocThiChon(), this.txtSearch.Text.Trim());
            ShowData();
        }
    }
}
